/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;
//import Connection.DBUtil;
import Bussiness.AdminDAO;
import Bussiness.EmployeeDAO;
import Connection.DBUtil;
import javax.swing.JOptionPane;
import Connection.Admin;
import Connection.CheckInfo;
import Connection.Employee;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
//import java.awt.HeadlessException;

/**
 *
 * @author SONY
 */
public class Login extends javax.swing.JDialog {
    /**
     * Creates new form Login
     */
    private Admin objAdmin = new Admin();
    private Employee objEmployee = new Employee();
    
    public Login(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        lblUser = new javax.swing.JLabel();
        lblPassword = new javax.swing.JLabel();
        txtuser = new javax.swing.JTextField();
        btnLogin = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        btnConfig = new javax.swing.JButton();
        lblDontconnect = new javax.swing.JLabel();
        rdAdmin = new javax.swing.JRadioButton();
        rdEmployee = new javax.swing.JRadioButton();
        jPassword = new javax.swing.JPasswordField();
        lblBackground = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setBackground(new java.awt.Color(240, 240, 240));
        setMinimumSize(new java.awt.Dimension(641, 400));
        getContentPane().setLayout(null);

        lblUser.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        lblUser.setText("Email");
        getContentPane().add(lblUser);
        lblUser.setBounds(230, 100, 50, 20);

        lblPassword.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        lblPassword.setText("Password");
        getContentPane().add(lblPassword);
        lblPassword.setBounds(230, 140, 70, 20);

        txtuser.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 102, 51)));
        txtuser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtuserActionPerformed(evt);
            }
        });
        getContentPane().add(txtuser);
        txtuser.setBounds(320, 100, 210, 20);

        btnLogin.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        btnLogin.setForeground(new java.awt.Color(0, 51, 51));
        btnLogin.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icons/16x16/accept.png"))); // NOI18N
        btnLogin.setText("Login");
        btnLogin.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });
        getContentPane().add(btnLogin);
        btnLogin.setBounds(230, 220, 90, 30);

        btnReset.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        btnReset.setForeground(new java.awt.Color(0, 51, 51));
        btnReset.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icons/16x16/refresh.png"))); // NOI18N
        btnReset.setText("Reset");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });
        getContentPane().add(btnReset);
        btnReset.setBounds(330, 220, 91, 30);

        btnCancel.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        btnCancel.setForeground(new java.awt.Color(0, 51, 51));
        btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icons/16x16/block.png"))); // NOI18N
        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        getContentPane().add(btnCancel);
        btnCancel.setBounds(430, 220, 100, 30);

        btnConfig.setBackground(new java.awt.Color(255, 255, 255));
        btnConfig.setFont(new java.awt.Font("Tahoma", 2, 12)); // NOI18N
        btnConfig.setForeground(new java.awt.Color(204, 0, 0));
        btnConfig.setText("Click here do config server ");
        btnConfig.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        btnConfig.setContentAreaFilled(false);
        btnConfig.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConfigActionPerformed(evt);
            }
        });
        getContentPane().add(btnConfig);
        btnConfig.setBounds(370, 270, 150, 30);

        lblDontconnect.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        lblDontconnect.setForeground(new java.awt.Color(0, 153, 51));
        lblDontconnect.setText("Don't connect,");
        getContentPane().add(lblDontconnect);
        lblDontconnect.setBounds(260, 270, 110, 30);

        rdAdmin.setBackground(new java.awt.Color(255, 255, 255));
        buttonGroup1.add(rdAdmin);
        rdAdmin.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        rdAdmin.setForeground(new java.awt.Color(0, 51, 51));
        rdAdmin.setText("Administrator");
        rdAdmin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdAdminActionPerformed(evt);
            }
        });
        getContentPane().add(rdAdmin);
        rdAdmin.setBounds(320, 180, 120, 23);

        rdEmployee.setBackground(new java.awt.Color(255, 255, 255));
        buttonGroup1.add(rdEmployee);
        rdEmployee.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        rdEmployee.setForeground(new java.awt.Color(0, 51, 51));
        rdEmployee.setText("Employee");
        rdEmployee.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdEmployeeActionPerformed(evt);
            }
        });
        getContentPane().add(rdEmployee);
        rdEmployee.setBounds(450, 180, 83, 23);

        jPassword.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 51, 51)));
        getContentPane().add(jPassword);
        jPassword.setBounds(320, 140, 210, 20);

        lblBackground.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icons/bglogin.jpg"))); // NOI18N
        getContentPane().add(lblBackground);
        lblBackground.setBounds(0, 0, 610, 340);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtuserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtuserActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtuserActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        // TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
        // TODO add your handling code here:
        txtuser.setText("");
        jPassword.setText("");
        txtuser.requestFocus();
    }//GEN-LAST:event_btnResetActionPerformed

    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed
        String email = txtuser.getText();
        String password = String.valueOf(jPassword.getPassword());
        Boolean b = false;
        DBUtil db = new DBUtil();
        if (!CheckInfo.checkEmpty(email)) {
            JOptionPane.showMessageDialog(null, "Please Enter Your Email");
        } else if (!CheckInfo.checkEmpty(password)){
            JOptionPane.showMessageDialog(null, "Please Enter Your Password");
        } else if (!rdEmployee.isSelected() && !rdAdmin.isSelected()) {
            JOptionPane.showMessageDialog(null, "Please Choice Rights Login");
        } else {
            if (rdEmployee.isSelected()){
                //check connect to database
                if ( db.connect() != null) {
                    objEmployee = EmployeeDAO.getLoginEmployee(email);
                    if (objEmployee != null) {
                        if (objEmployee.getEmail().equals(email) && objEmployee.getPassword().equals(password)) {
                            b = true;
                            JOptionPane.showMessageDialog(null, "Login successful" );
                            EmployeeMain mainE;
                            try {
                                mainE = new EmployeeMain();
                                this.dispose();
                                mainE.setLocationRelativeTo(mainE);
                                mainE.setVisible(true);
                                mainE.loadEmployee(objEmployee);
                            } catch (Exception ex) {
                                Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        } else {
                            JOptionPane.showMessageDialog(null, "Wrong Password!");
                            txtuser.setText("");
                            jPassword.setText("");
                            txtuser.requestFocus();
                        }
                    } else {
                        JOptionPane.showMessageDialog(null, "Wrong Email!");
                        txtuser.setText("");
                        jPassword.setText("");
                        txtuser.requestFocus();
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "Don't conect to sever");
                }
            }

            if (rdAdmin.isSelected()) {
                //check connect to database
                if ( db.connect() != null) {
                    objAdmin = AdminDAO.getAllAdmin(email);
                    if (objAdmin != null) {
                        if (objAdmin.getEmail().equals(email) && objAdmin.getPassword().equals(password)) {
                            b = true;
                            JOptionPane.showMessageDialog(null, "Login successful" );
                            AdminMain mainA = null;
                            try {
                            mainA = new AdminMain();
                            this.dispose();
                            mainA.setLocationRelativeTo(mainA);
                            mainA.setVisible(true);
                            mainA.loadAdmin(objAdmin);
                            //mainA.loadAdmin(objAdmin);
                            } catch (Exception ex) {
                                Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        } else {
                            JOptionPane.showMessageDialog(null, "Wrong password!");
                            jPassword.setText("");
                            jPassword.requestFocus();
                        }
                    } else {
                        JOptionPane.showMessageDialog(null, "Wrong email!");
                        txtuser.setText("");
                        jPassword.setText("");
                        txtuser.requestFocus();
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "Don't connect to sever");
                }
            }
        }
    }//GEN-LAST:event_btnLoginActionPerformed

    private void rdAdminActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdAdminActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_rdAdminActionPerformed

    private void rdEmployeeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdEmployeeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_rdEmployeeActionPerformed

    private void btnConfigActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConfigActionPerformed
        // TODO add your handling code here:
        Config cfg = new Config();
        this.dispose();
        cfg.setLocationRelativeTo(this);
        cfg.setVisible(true);
    }//GEN-LAST:event_btnConfigActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Login dialog = new Login(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setLocationRelativeTo(dialog);
                dialog.setVisible(true);

            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnConfig;
    private javax.swing.JButton btnLogin;
    private javax.swing.JButton btnReset;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JPasswordField jPassword;
    private javax.swing.JLabel lblBackground;
    private javax.swing.JLabel lblDontconnect;
    private javax.swing.JLabel lblPassword;
    private javax.swing.JLabel lblUser;
    private javax.swing.JRadioButton rdAdmin;
    private javax.swing.JRadioButton rdEmployee;
    private javax.swing.JTextField txtuser;
    // End of variables declaration//GEN-END:variables
}
